var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    productList: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    dealType:0, 
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this
    try { 
      var _dealType = wx.getStorageSync('dealType');
      this.setData({
        dealType: _dealType
      });
    } catch (e) {}
  },
  getProductList() {
    // 页面显示 
    wx.showLoading({
      title: '加载中...',
    });   
    let that = this;
    util.request(api.MyProductsList, {
      dealType: that.data.dealType,
      currentPage: that.data.page,
      pageSize: that.data.limit
    }).then(function(res) {
      if (res.code === 200) {
        console.log(res.dataObject);
        that.setData({
          productList: res.dataObject,
          totalPages: res.pages
        });
      }
      wx.hideLoading();
    });
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getProductList();
    } else {
      wx.showToast({
        title: '没有更多产品了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  switchTab: function(event) {
    let _dealType = event.currentTarget.dataset.index;
    this.setData({
      productList: [],
      dealType: _dealType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getProductList();
  },
  onClickEdit(e){
    console.log(e);
    let productId = e.currentTarget.dataset.productId

    wx.navigateTo({   
      url: './../editProduct/editProduct?productId='+productId,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  onClickOperation(e){
    let _status = e.currentTarget.dataset.status;
    let _productId = e.currentTarget.dataset.productId;
    
    var _itemList = [];
    if(_status!=1){
      _itemList.push("上架");
    }
    if(_status!=2){
      _itemList.push("下架");
    }
    if(_status!=5){
      _itemList.push("删除");
    }
    var that = this;
    wx.showActionSheet({
      itemList: _itemList,
      success: function (res) {
        let _tapIndex = res.tapIndex;
        var _updateStatus = 0;
        for (let index = 0; index < _itemList.length; index++) {
          const element = _itemList[index];
          if(index===_tapIndex){
            if(element==="上架"){
              _updateStatus = 1;
              break;
            }
            if(element==="下架"){
              _updateStatus = 2;
              break;
            }
            if(element==="删除"){
              _updateStatus = 5;
              break;
            }
            break;
          } 
        } 
        console.log(_updateStatus);
        console.log(_productId);

        util.request(api.ProductUpdateStatus, {
          updateStatus:_updateStatus,
          productId:_productId
        }, 'POST').then(function(res) { 
          if (res.code === 200) {
             that.getProductList();
          } 
          wx.hideLoading();
        });
      }
    });

  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    this.getProductList();
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})